﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Configuration;
namespace MonitorPlan.Admin
{
    public partial class Config : Page
    {
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if(Session["UserId"] ==null)
                {
                    Response.Redirect("/Admin");
                }
                if(Session["new_location"]!=null)
                {
                    this.mv.ActiveViewIndex = 1;
                    Session.Remove("new_location");
                }
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            mv.ActiveViewIndex=0;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            mv.ActiveViewIndex = 1;
            this.txtLocationName.Text = "";
            this.txtDescription.Text = "";
            this.actionSms.InnerHtml = "";
            this.txtLocationName.Focus();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String sms; 

            Model.Location lc = new Model.Location();
            lc.Name = this.txtLocationName.Text;
            lc.Description = this.txtDescription.Text;
            if(this.txtLocationName.Text !="")
            {
                if (lc.Add())
                {
                    sms = "<div class='alert alert-success'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has been save successfully!";
                    sms += "</div>";
                    this.txtLocationName.Text = "";
                    this.txtDescription.Text = "";
                    this.txtLocationName.Focus();
                    this.actionSms.InnerHtml = sms;
                }
                else
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has not been saved successfully. Try again!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
            }
            else
            {
                this.actionSms.InnerHtml = "<span style='color:red'>Please input location name!</span>";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtLocationName.Text = "";
            this.txtDescription.Text = "";
            this.actionSms.InnerHtml = "";
            this.txtLocationName.Focus();
        }

        protected void rp1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "detail")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.Location lc = new Model.Location().getLocation(id);
                this.lb1.Text = lc.Id.ToString();
                this.lb2.Text = lc.Name;
                this.lb3.Text = lc.Description;
                this.lb4.Text = lc.IsDeleted.ToString();
                this.mv.ActiveViewIndex = 2; 
                
            }
            if(e.CommandName =="edit")
            {
                this.mv.ActiveViewIndex = 3;
                int id = Convert.ToInt32(e.CommandArgument);
                
                Model.Location lc = new Model.Location().getLocation(id);
                this.txt1.Text = lc.Name;
                this.txt2.Text = lc.Description;
                this.txt3.Value = id.ToString();
            }
            if(e.CommandName=="delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Model.Location lc = new Model.Location();
                if (lc.Remove(id)) {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if(this.txt1.Text != "")
            {
                Model.Location lc = new Model.Location();
                lc.Name = this.txt1.Text;
                lc.Description = this.txt2.Text;
                int id = Convert.ToInt32(this.txt3.Value);
                if(lc.Update(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
                else
                {
                   
                    this.updateSms.InnerHtml = "Cannot update data. Try again!";
                }
            }
        }

        protected void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }
    }
}